package com.sky.service.impl;

import com.sky.dto.DataOverViewQueryDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.utils.TimeUtil;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/22 18:17
 * @Version 1.0.0
 */
@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(DataOverViewQueryDTO dataOverViewQueryDTO) throws Exception {
        TurnoverReportVO reportVO = new TurnoverReportVO();
        String DateList = new String();
        String TurnoverList = new String();
        List<String> betweenDay = TimeUtil.getBetweenDay(dataOverViewQueryDTO.getBegin(), dataOverViewQueryDTO.getEnd());
        log.info("betweenDay: " + betweenDay);
        for (String time : betweenDay) {
            DateList = DateList + time +",";
            Integer sumAmount = orderMapper.sumAmount(time, Orders.COMPLETED);
            if (sumAmount == null){
                sumAmount = 0;
            }
            TurnoverList= TurnoverList + sumAmount +",";
        }
        reportVO.setDateList(DateList);
        reportVO.setTurnoverList(TurnoverList);
        return reportVO;
    }

    @Override
    public UserReportVO userStatistics(DataOverViewQueryDTO dataOverViewQueryDTO) throws Exception {
        UserReportVO userReportVO = new UserReportVO();
        String DateList = new String("");
        String totalUserList = new String("");
        String newUserList = new String("");

        List<String> betweenDay = TimeUtil.getBetweenDay(dataOverViewQueryDTO.getBegin(), dataOverViewQueryDTO.getEnd());
        log.info("betweenDay: " + betweenDay);
        for (String time : betweenDay) {
            DateList = DateList + time +",";
            String countByTime = userMapper.getTotalCount(time);
            totalUserList = totalUserList + countByTime + ",";

            String num = userMapper.getAddCount(time);
            newUserList = newUserList + num + ",";
        }
        userReportVO.setDateList(DateList);
        userReportVO.setNewUserList(newUserList);
        userReportVO.setTotalUserList(totalUserList);
        return userReportVO;
    }

    @Override
    public SalesTop10ReportVO top10(DataOverViewQueryDTO dataOverViewQueryDTO) throws Exception {
        //首先创建一个返回类型
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        dataOverViewQueryDTO.setBegin(dataOverViewQueryDTO.getBegin()+" 00:00:00");
        dataOverViewQueryDTO.setEnd(dataOverViewQueryDTO.getEnd()+" 23:59:59");
        //声明返回参数
        String nameList = new String("");
        String numberList = new String("");
        //获取开始时间与结束时间之间的日期
        List<String> betweenDay = TimeUtil.getBetweenDay(dataOverViewQueryDTO.getBegin(), dataOverViewQueryDTO.getEnd());
        List<SalesTop10ReportVO> resp = orderMapper.top10(dataOverViewQueryDTO);
        for (SalesTop10ReportVO reportVO : resp) {
            nameList = nameList + reportVO.getNameList() + ",";
            numberList = numberList + reportVO.getNumberList() + ",";
        }
        salesTop10ReportVO.setNameList(nameList);
        salesTop10ReportVO.setNumberList(numberList);
        return salesTop10ReportVO;
    }

    @Override
    public OrderReportVO ordersStatistics(DataOverViewQueryDTO dataOverViewQueryDTO) throws Exception {
        OrderReportVO orderReportVO = new OrderReportVO();
        //日期，每日订单数,每日有效订单数以逗号分隔
        String dateList = new String();
        String orderCountList = new String();
        String validOrderCountList = new String();
        //获取开始时间与结束时间之间的日期
        List<String> betweenDay = TimeUtil.getBetweenDay(dataOverViewQueryDTO.getBegin(), dataOverViewQueryDTO.getEnd());
        for (String time : betweenDay) {
            //返回时间格式
            dateList = dateList + time +",";
            OrderReportVO reportVO = orderMapper.allOrdersStatistics(time);
            orderCountList = orderCountList + reportVO.getOrderCountList() + ",";
            validOrderCountList = validOrderCountList + reportVO.getValidOrderCountList() + ",";
        }
        //将结果进行返回
        orderReportVO.setDateList(dateList);
        orderReportVO.setOrderCountList(orderCountList);
        orderReportVO.setValidOrderCountList(validOrderCountList);
        //下面查询这一段时间类的订单个数以及
        dataOverViewQueryDTO.setBegin(dataOverViewQueryDTO.getBegin()+" 00:00:00");
        dataOverViewQueryDTO.setEnd(dataOverViewQueryDTO.getEnd()+" 23:59:59");
        //查询这段时间的数据
        OrderReportVO reportVO = orderMapper.OrdersStatistics(dataOverViewQueryDTO);
        //将结果进行返回
        orderReportVO.setTotalOrderCount(reportVO.getTotalOrderCount());
        orderReportVO.setValidOrderCount(reportVO.getValidOrderCount());
        orderReportVO.setOrderCompletionRate(Double.valueOf(reportVO.getValidOrderCount()*1.0/reportVO.getTotalOrderCount()));
        return orderReportVO;
    }
}
